/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Estatistica;

/**
 *
 * @author fosa
 */
import java.util.Arrays;
import java.util.HashMap;

public class Estatistica {

    // Coeficiente de Variação de Pearson
    public static double getPearson(double[] array) {
        return (getDesvioPadrao(array) / getMediaAritmetica(array)) * 100;
    }

    public static double getMediaAritmetica(double[] array) {

        double total = 0;

        for (int counter = 0; counter < array.length; counter++) {
            total += array[counter];
        }
        return total / array.length;
    }

    public static double getSomaDosElementos(double[] array) {

        double total = 0;

        for (int counter = 0; counter < array.length; counter++) {
            total += array[counter];
        }
        return total;
    }

    public static double getSomaDosElementosAoQuadrado(double[] array) {

        double total = 0;

        for (int counter = 0; counter < array.length; counter++) {
            total += Math.pow(array[counter], 2);
        }
        return total;
    }

    public static void ordenar(double[] array) {
        Arrays.sort(array);
    }

    public static int buscaPor(int value, double[] array) {

        return Arrays.binarySearch(array, value);

    }

    // Variância
    public static double getVariancia(double[] array) {

        double p1 = 1 / Double.valueOf(array.length - 1);

        double p2 = getSomaDosElementosAoQuadrado(array) - (Math.pow(getSomaDosElementos(array), 2) / Double.valueOf(array.length));

        return p1 * p2;
    }

    // Desvio Padrão
    public static double getDesvioPadrao(double[] array) {

        return Math.sqrt(getVariancia(array));
    }

    public static double getMediana(double[] array) {

        ordenar(array);
        int tipo = array.length % 2;

        if (tipo == 1) {

            return array[((array.length + 1) / 2) - 1];

        } else {
            int m = array.length / 2;

            return (array[m - 1] + array[m]) / 2;
        }
    }

    public static double getModa(double[] array) {
        HashMap map = new HashMap();

        Integer i;

        Double moda = 0.0;

        Integer numAtual, numMaior = 0;

        for (int count = 0; count < array.length; count++) {

            i = (Integer) map.get(new Double(array[count]));

            if (i == null) {

                map.put(new Double(array[count]), new Integer(1));

            } else {

                map.put(new Double(array[count]), new Integer(i.intValue() + 1));

                numAtual = i.intValue() + 1;

                if (numAtual > numMaior) {

                    numMaior = numAtual;

                    moda = new Double(array[count]);

                }
            }
        }
        return moda;
    }

    public static double getCoefAssimetria(double[] array) {
        return (getMediaAritmetica(array) - getModa(array)) / getDesvioPadrao(array);
    }
}

